package com.minelx.woods.server.entity.values;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.bson.Document;

import java.util.Objects;

public final class TaskValues {
	private final String id;

	private final Integer condition;

	private final JSONObject info;

	public TaskValues(String id, Integer condition, JSONObject info) {
		this.id = id;
		this.condition = condition;
		this.info = info;
	}

	public String getId() {
		return id;
	}

	public Integer getCondition() {
		return condition;
	}

	public JSONObject getInfo() {
		return info;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		TaskValues that = (TaskValues) o;
		return id.equals(that.id) && condition.equals(that.condition) && info.equals(that.info);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id, condition, info);
	}

	@Override
	public String toString() {
		return "TaskValues{" +
				"id='" + id + '\'' +
				", condition=" + condition +
				", info=" + info +
				'}';
	}

	public static TaskValues from(Document found) {
		return new TaskValues(
				found.get("_id").toString(),
				found.getInteger("condition"),
				JSON.parseObject(found.get("info", Document.class).toJson()));
	}
}
